<?php

namespace plugin\ycMassage\utils\sms;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use \Exception;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use app\common\manager\SettingsMgr;

class Aliyun
{
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret)
    {
        $config = new Config([
            // 必填，您的 AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 必填，您的 AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * @param string[] $args
     * @return bool
     */
    public static function main($saas_appid, $mobile, $code)
    {
        $config = SettingsMgr::getChildren($saas_appid, 'sms');
        if (empty($config['aliyun_accessKeyId']) || empty($config['aliyun_accessKeySecret'])) {
            throw new Exception("请先配置阿里云短信");
        }
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html
        $client = self::createClient($config['aliyun_accessKeyId'], $config['aliyun_accessKeySecret']);
        $request = new SendSmsRequest();
        $request->phoneNumbers = $mobile;
        $request->signName = $config['aliyun_signName'];
        $request->templateCode = $config['aliyun_TemplateCode'];
        $request->templateParam = json_encode(['code' => $code], JSON_UNESCAPED_UNICODE);
        $runtime                 = new RuntimeOptions();
        $runtime->maxIdleConns   = 3;
        $runtime->connectTimeout = 10000;
        $runtime->readTimeout    = 10000;
        // 复制代码运行请自行打印 API 的返回值
        $res = $client->sendSms($request, $runtime);
        if ($res->body->code == 'OK') {
            return true;
        }
        if ($res->body->message) {
            throw new Exception($res->body->message);
        }
        throw new Exception("发送失败");
    }
    public static function send($saas_appid, $mobile, $templateCode, $data)
    {
        $config = SettingsMgr::getChildren($saas_appid, 'sms');
        if (empty($config['aliyun_accessKeyId']) || empty($config['aliyun_accessKeySecret'])) {
            throw new Exception("请先配置阿里云短信");
        }
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html
        $client = self::createClient($config['aliyun_accessKeyId'], $config['aliyun_accessKeySecret']);
        $request = new SendSmsRequest();
        $request->phoneNumbers = $mobile;
        $request->signName = $config['aliyun_signName'];
        $request->templateCode = $templateCode;
        if (!empty($data)) {
            $request->templateParam = json_encode($data, JSON_UNESCAPED_UNICODE);
        }
        $runtime                 = new RuntimeOptions();
        $runtime->maxIdleConns   = 3;
        $runtime->connectTimeout = 10000;
        $runtime->readTimeout    = 10000;
        // 复制代码运行请自行打印 API 的返回值
        $res = $client->sendSms($request, $runtime);
        if ($res->body->code == 'OK') {
            return true;
        }
        if ($res->body->message) {
            throw new Exception($res->body->message);
        }
        throw new Exception("发送失败");
    }
}
